<template>
	<view>
		<view class="u-content">
			<u-parse :key="`content${Date.now()}`" :content="content"></u-parse>
			<!-- <u--text :text="this.content" color="red" customStyle="margin: 20rpx 10px 0px 20rpx"></u--text> -->
		</view>

		<u--text text="* 添加审核图片" color="red" customStyle="margin: 20rpx 10px 0px 20rpx"></u--text>
		<u-upload style="margin: 20rpx;" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
			multiple :maxCount="9"></u-upload>

		<u--textarea customStyle="margin: 10rpx 20rpx 30rpx 20rpx" v-model="inputValue"
			placeholder="请输入提交信息"></u--textarea>

		<view class="custom-style-parent">
			<u-button @click="submitClick" class="custom-style" type="error" text="提交"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '222',
				fileList1: [],
				inputValue: '',
				proTitle: '',
				proId: '',
				proMoney: '',
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://47.119.155.244:8082/public/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						// formData: {
						// 	user: 'test'
						// },
						success: (res) => {
							setTimeout(() => {
								resolve(res)
							}, 1000)
						}
					});
				})
			},
			submitClick() {
				let imageList = []
				for (var i = 0; i < this.fileList1.length; i++) {
					imageList.push(this.fileList1[i].url.data)
				}
				console.log("submitClick", "imageList=" + imageList)
				let param = {
					proId: this.proId,
					proTitle: this.proTitle,
					proMoney: this.proMoney,
					screenshotLsit: imageList,
					illustrate: this.inputValue,
				}
				uni.showLoading({
					title: '请求中...'
				})
				this.$u.api.apiAddProjectRecord(param).then(res => {
					console.log('apiAddProjectRecord', res);
					uni.hideLoading();
					if (res.code == 1) {
						uni.$u.toast("提交成功!");
						uni.navigateBack();
					} else {
						uni.$u.toast(res.msg);
					}
				})
			},
		},
		onLoad(options) {
			// this.content = '`'+options.proContent+'`';
			this.proTitle = options.proTitle;
			this.proId = options.proId;
			this.proMoney = options.proMoney;
			console.log("onLoad", this.proId + '--' + this.proTitle + "--" + this.proMoney + "--" + this.content)
			this.content = JSON.parse(uni.getStorageSync('proContent'))
		},
	}
</script>

<style>
	.u-content {
		padding: 20rpx;
	}

	.custom-style-parent {
		display: flex;
		justify-items: center;
		justify-content: center;
		margin-top: 100rpx;
		margin-left: 30rpx;
		margin-right: 30rpx;
		height: 300rpx;
	}
</style>